package com.example.mapsapp;

import java.util.ArrayList;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.LocationSource;
import com.google.android.gms.maps.SupportMapFragment;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Map extends FragmentActivity implements LocationListener{

	 
	private GoogleMap map;
	private LocationManager locationManager;
	private static final long MIN_TIME = 400;
	private static final float MIN_DISTANCE = 1000;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.map_activity);
		
	    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

		 
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.map_activity, menu);
	    return true;
	}
	
	  public boolean onOptionsItemSelected(MenuItem item) {
		    switch(item.getItemId())
		    {
		    case R.id.locationOption:
		    	getLocation();
				return true;
		    case R.id.mapHybrid:
		    	mapTypeHybrid();
		    	return true;
	   		case R.id.mapSat:
	    	mapTypeSat();
	    	return true;
	  		case R.id.mapTerrain:
	    	mapTypeTerrain();
	    	return true;
	  		case R.id.mapNormal:
		    	mapTypeNormal();
		    	return true;
			}
			return true;
		}
	
	  public void getLocation(){
		    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
		   
	  }
	  
	  public void mapTypeHybrid(){
		  map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
	  }
	  
	  public void mapTypeSat(){
		  map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
	  }
	  
	  public void mapTypeTerrain(){
		  map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
	  }
	  
	  public void mapTypeNormal(){
		  map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
	  }
	

	@Override
	public void onLocationChanged(Location location) {
	    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
	    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
	    map.animateCamera(cameraUpdate);
	    locationManager.removeUpdates(this);
	    
	    map.addMarker(new MarkerOptions()
    	.title("My Location")
    	.position(latLng)
    	.snippet("Coordinates: " + location.getLatitude() + ", " + location.getLongitude())
    	.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
    		);
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) { }

	@Override
	public void onProviderEnabled(String provider) { }

	@Override
	public void onProviderDisabled(String provider) { }

	}



